package com.hansen.library.ui.widget.banner.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class BannerViewPager extends ViewPager {

    private boolean scrollable = true;
    private boolean isShow;

    private float mRatio;

    public BannerViewPager(Context context) {
        super(context);
        isShow = true;
    }

    public BannerViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        isShow = true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(mRatio > 0f) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            setMeasuredDimension(width, (int) (mRatio * width));
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return this.scrollable && super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return this.scrollable && super.onInterceptTouchEvent(ev);
    }

    public void setScrollable(boolean scrollable) {
        this.scrollable = scrollable;
    }

    public void setRatio(float mRatio) {
        this.mRatio = mRatio;
    }

    /*@Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        try {
            Field mFirstLayout = ViewPager.class.getDeclaredField("mFirstLayout");
            mFirstLayout.setAccessible(true);
            if (isShow) {
                isShow = false;
                mFirstLayout.set(this, true);
            } else {
                mFirstLayout.set(this, false);
            }
            getAdapter().notifyDataSetChanged();
            setCurrentItem(getCurrentItem());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
